package com.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static java.lang.Thread.sleep;

/**
 * interrupt方法详解
 */
@Slf4j
public class InterruptMethod {
    /**
     * 让线程进入阻塞状态的方法有三种
     * sleep
     * wait
     * join
     * 一旦interrupt方法打断处于这三个方法导致的阻塞状态时，会抛出InterrutpedException异常
     * 并且将中断标志重置为false
     * 这里先演示sleep导致的阻塞
     * TODO 每个线程都有一个方法，是Thread类的静态方法 interrupted
     * TODO 静态方法interrupted()会返回当前线程的中断状态，并重置中断状态为false
     * 一些方法不推荐使用，容易破坏同步代码块，造成线程死锁
     * stop()   暴力停止，可能导致某些线程的收尾工作没有完成
     * suspend()    挂起（暂停）线程运行
     * resume()     恢复线程运行
     */

    public static void main(String[] args) throws InterruptedException {
        log.debug("sleep方法线程");
        testSleepInterrupt();
        log.debug("wait方法线程");
        testWaitInterrupt();
        log.debug("join方法线程");
        testJoinInterrupt();
        log.debug("park方法线程");
        testParkInterrupt();
        log.debug("正常方法线程");
        testNormalInterrupt();
    }

    /**
     * 打断sleep的线程
     *
     * @throws InterruptedException
     */
    public static void testSleepInterrupt() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
        log.debug("testSleepInterrupt打断前t1的状态:{}", t1.isInterrupted());
        sleep(500);
        t1.interrupt();
        sleep((500));
        log.debug("testSleepInterrupt打断后t1的状态:{}", t1.isInterrupted());
    }

    /**
     * 打断wait的线程
     *
     * @throws InterruptedException
     */
    public static void testWaitInterrupt() throws InterruptedException {
        final Object lock = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1");
        t1.start();
        log.debug("testWaitInterrupt打断前t1的状态: {}", t1.isInterrupted());
        Thread.sleep(500);
        t1.interrupt();
        Thread.sleep(500);
        log.debug("testWaitInterrupt打断后t1的状态: {}", t1.isInterrupted());
    }

    /**
     * 打断join的线程
     *
     * @throws InterruptedException
     */
    public static void testJoinInterrupt() throws InterruptedException {
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");
        t2.start();

        Thread t3 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3");
        t3.start();
        log.debug("testJoinInterrupt打断前t3的状态: {}", t3.isInterrupted());
        Thread.sleep(500);
        t3.interrupt();
        Thread.sleep(500);
        log.debug("testJoinInterrupt打断后t3的状态: {}", t3.isInterrupted());
    }

    /**
     * 打断park线程
     * 不会清空打断状态
     * park方法:park方法是java.util.concurrent.locks.LockSupport类中的一个静态方法
     * 用于暂停当前线程，使其进入阻塞状态，直到被其他线程通过unpark方法唤醒、或被其他线程使用interrupt方法中断
     */
    public static void testParkInterrupt() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            //线程暂停，要想唤醒，必须其他线程调用unpark方法
            LockSupport.park();
            log.debug("unpark...");
            Thread thread = Thread.currentThread();
            boolean interrupted = thread.isInterrupted();
            while (true) {
                if (interrupted) {
                    log.debug("打断状态:{}", interrupted);
                    break;
                }
            }
        });
        t1.start();
        log.debug("testParkInterrupt打断前状态:{}", t1.isInterrupted());
        t1.interrupt();
        sleep(500);
        log.debug("testParkInterrupt打断后状态:{}", t1.isInterrupted());
    }

    /**
     * 打断正常运行的线程
     *
     * @throws InterruptedException
     */
    public static void testNormalInterrupt() throws InterruptedException {
        Thread t2 = new Thread(() -> {
            while (true) {
                Thread thread = Thread.currentThread();
                boolean interrupted = thread.isInterrupted();
                if (interrupted) {
                    log.debug("打断状态:{}", interrupted);
                    break;
                }
            }
        }, "t1");
        t2.start();
        log.debug("testNormalInterrupt打断前t1的状态:{}", t2.isInterrupted());
        sleep((long) 0.5);
        t2.interrupt();
        log.debug("testNormalInterrupt打断后t1的状态:{}", t2.isInterrupted());
    }
}
